package com.ruoyi.common.utils.aliyun;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aliyuncs.CommonResponse;
import com.doudou.et.aliyun.sms.AliyunSms;
import com.doudou.et.aliyun.sms.SmsTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;

/**
 * @author zzx
 */
@Component
public class MySms {

    @Value("${aliyunsms.regionId}") private String regionId;
    @Value("${aliyunsms.accessKey}") private String accessKey;
    @Value("${aliyunsms.accessSecret}") private String accessSecret;
    @Value("${aliyunsms.domain}") private String domain;
    @Value("${aliyunsms.codeSign}") private String codeSign;
    @Value("${aliyunsms.commonSign}") private String commonSign;
    @Value("${aliyunsms.phoneCodeTemplateCode}") private String phoneCodeTemplateCode;//手机验证码
    @Value("${aliyunsms.noticeCodeTemplateCode}") private String noticeCodeTemplateCode;
    @Value("${aliyunsms.exApplyTemplateCode}") private String exApplyTemplateCode;//考试报名
    @Value("${aliyunsms.classApplyTemplateCode}") private String classApplyTemplateCode;//班级报名
    @Value("${aliyunsms.firstCertTemplateCode}") private String firstCertTemplateCode;//初训结果反馈通知
    @Value("${aliyunsms.reCertTemplateCode}") private String reCertTemplateCode;//复训结果反馈通知
    @Value("${aliyunsms.reOpenClassTemplateCode}") private String reOpenClassTemplateCode;//复训开班通知
    @Value("${aliyunsms.firstAppointTemplateCode}") private String firstAppointTemplateCode;//初训实操通知
    @Value("${aliyunsms.firstExTemplateCode}") private String firstExTemplateCode;//初训操作考试通知

    /**
     * 整合阿里云短信服务 设置相关参数
     */
    private AliyunSms aliyunSms;

    /**
     * 初始化发送对象
     */
    @PostConstruct
    private void init(){
        this.aliyunSms = AliyunSms.builder().regionId(regionId).accessKey(accessKey).accessSecret(accessSecret).domain(domain).build();
    }

    /**
     * 获取短信模板
     */
    public List<SmsTemplate> getAllTemplate(){
        return aliyunSms.getAllTemplate();
    }

    public CommonResponse sendSmsSingle(String templateCode, Map<String,Object> map, Object...phones){
        return aliyunSms.sendSmsSingle(commonSign,templateCode,map,phones);
    }

        /**
         * 发送通告-群发
         */
    public CommonResponse sendNotice(JSONObject templateParams,String...phones){
        return aliyunSms.sendSmsMass(codeSign,noticeCodeTemplateCode,templateParams,phones);
    }

    /**
     * 发送短信验证码
     */
    public CommonResponse sendPhoneCode(String phone,String code){
        return aliyunSms.sendSmsSingle(codeSign,phoneCodeTemplateCode,obj().set("code",code),phone);
    }

    /**
     * 发送考试报名
     * 尊敬的 ${name} 您已成功报名${text}，考试日期为${date}，时间段为${time}，期待与您的相遇！ 公卫处方权培训中心欢迎您!
     */
    public CommonResponse sendExApply(String phone,String name,String text,String date,String time){
        return aliyunSms.sendSmsSingle(commonSign,exApplyTemplateCode, obj().set("name",name).set("text",text).set("date",date).set("time",time), phone);
    }

    /**
     * 发送班级报名
     * 尊敬的 ${name} 您已成功报名${text}，开班日期为${date}，期待与您的相遇！ 公卫处方权培训中心欢迎您!
     */
    public CommonResponse sendClassApply(String phone,String name,String text,String date){
        return aliyunSms.sendSmsSingle(commonSign,classApplyTemplateCode, obj().set("name",name).set("text",text).set("date",date), phone);
    }

    /**
     * 发送初训结果反馈通知
     * ${name}您好，您已完成${year}产褥期保健上岗证，考核合格，上岗证有效期延至下一年颁证日期，上岗证编号为${num}。
     */
    public CommonResponse sendFirstCert(String phone,String name,int year,String num){
        return aliyunSms.sendSmsSingle(commonSign,firstCertTemplateCode,
                obj().set("name",name).set("year",year).set("num",num),phone);
    }

    /**
     * 发送复训结果反馈通知
     * ${name}您好，您已完成${year}产褥期保健上岗证，考核合格，上岗证有效期延至下一年颁证日期
     */
    public CommonResponse sendReCert(String phone,String name,int year){
        return aliyunSms.sendSmsSingle(commonSign,reCertTemplateCode, obj().set("name",name).set("year",year),phone);
    }

    /**
     * 复训开班通知
     * ${name}您好，${year}产褥期保健复训班计划于${date},${time}开班，请在${date1}前,登录微信小程序“公卫处方权培训基地”，完善个人信息，进行线上报名，并按期完成培训和考试。小程序登录名为上岗证编号
     */
    public CommonResponse sendReOpenClass(String phone,String name,int year,String date,String time,String date1){
        return aliyunSms.sendSmsSingle(commonSign,reOpenClassTemplateCode, obj().set("name",name).set("year",year).set("date",date).set("time",time).set("date1",date1),phone);
    }

    /**
     * 初训实操通知
     * ${name}您好，${year}产褥期保健实训基地计划于${date}至${date1},每日${time}开放，请登录微信小程序“公卫处方权培训基地”，进行线上预约，并按期参加。
     */
    public CommonResponse sendFirstAppoint(String phone,String name,int year,String date,String date1,String time){
        return aliyunSms.sendSmsSingle(commonSign,firstAppointTemplateCode, obj().set("name",name).set("year",year).set("date",date).set("time",time).set("date1",date1),phone);
    }

    /**
     * 初训操作考试通知
     * ${name}您好，${year}产褥期保健上岗培训班的操作考试将于${date}至${date1},每日${time}进行。请登录微信小程序“公卫处方权培训基地”，进行线上预约，并按期参加。
     */
    public CommonResponse sendFirstEx(String phone,String name,int year,String date,String date1,String time){
        return aliyunSms.sendSmsSingle(commonSign,firstExTemplateCode, obj().set("name",name).set("year",year).set("date",date).set("time",time).set("date1",date1),phone);
    }

    /**
     * 线上失误
     * ${name}您好，上述开班信息为测试信息，请勿再进行实体操作，十分抱歉！麻烦大家等各个社区OA通知和再次统一短信告知，十分感谢！
     */
    public CommonResponse sendSave(String phone,String name){
        return aliyunSms.sendSmsSingle(commonSign,"SMS_462615289", obj().set("name",name),phone);
    }

    private JSONObject obj(){
        return new JSONObject();
    }

    public static void main(String[] args) {

        AliyunSms sms = AliyunSms.builder().regionId("cn-hangzhou").accessKey("LTAI5tA341sk8Mq21NKZeAZm").accessSecret("7bWPdpCQ15ME6YqHATSdxaMqSvOLYD").domain("dysmsapi.aliyuncs.com").build();

        System.out.println(JSONUtil.toJsonPrettyStr(sms.getAllTemplate()));
//        sms.sendSmsSingle("浦东新区妇幼保健中心","SMS_462016109",new JSONObject(){{
//            set("name","倪宇");
//            set("year","2023");
//            set("date","2023-01-01");
//            set("date1","2023-02-1");
//            set("time","08:00:00");
//        }},"15601873865");
    }


}
